<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>Hello APP</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
    <link rel="stylesheet" type="text/css" href="../css/aui.css" />
    <style>
        #notice{ overflow: hidden; white-space:nowrap; text-overflow: ellipsis; }
    </style>
</head>
<body>
    <section class="aui-content">
        <div class="aui-card-list">
            <div class="aui-card-list-content">
                <img src="../image/l2.png" />
            </div>
            <div class="aui-card-list-footer" onclick="openWin('announcement_detail')">
                <div id="notice">
                    <span>公告 ： </span>
                    <span id="noticeContent"></span>
                </div>
            </div>
        </div>
    </section>
    <section class="aui-content aui-margin-b-10">
        <div class="aui-grid">
            <div class="aui-row">
                <div class="aui-col-xs-3" onclick="openWin('check_work')">
                    <i  class="aui-iconfont aui-icon-map aui-text-info"></i>
                    <div class="aui-grid-label">打卡</div>
                </div>
                <div class="aui-col-xs-3" onclick="openWin('daily')">
                    <i  class="aui-iconfont aui-icon-pencil aui-text-warning"></i>
                    <div class="aui-grid-label">写日报</div>
                </div>
                <div class="aui-col-xs-3" onclick="openWin('file')">
                    <i  class="aui-iconfont aui-icon-edit aui-text-warning"></i>
                    <div class="aui-grid-label">文档</div>
                </div>
                <div class="aui-col-xs-3" onclick="openWin('workflow')">
                    <i  class="aui-iconfont aui-icon-share aui-text-info"></i>
                    <div class="aui-grid-label">工作流</div>
                </div>
                <div class="aui-hr aui-clearfix"></div>
            </div>
        </div>
    </section>
    <div class="aui-content aui-margin-b-15">
        <ul class="aui-list aui-media-list" id="daily-list">

        </ul>
    </div>

</body>
<script type="text/javascript" src="../script/zepto.min.js" ></script>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/aui-toast.js" ></script>
<script type="text/javascript">
var toast = new auiToast();
apiready = function(){
     $api.setStorage("daily_index", 0);
     load();
     getAnnouncement();
     //下拉刷新
     api.setRefreshHeaderInfo({
         visible: true,
         loadingImg: 'widget://image/refresh.png',
         bgColor: '#ccc',
         textColor: '#fff',
         textDown: '下拉刷新...',
         textUp: '松开刷新...',
         showTime: true
     }, function(ret, err){
         window.location.reload();
         api.refreshHeaderLoadDone();
         //$('#tips-1').show();
         //setTimeout(closeTips,2000);

     });
     //上拉翻页
     api.addEventListener({
         name : 'scrolltobottom'
     }, function(ret, err) {
         $api.setStorage("daily_index", $api.getStorage("daily_index") - 0 + 1);
         load();
     });
     api.addEventListener({
         name:'loginSuccess'
     },function(ret,err){
         window.location.reload();
         //coding...
     });
 }

// 加载公告
function getAnnouncement () {
    api.ajax({
        url: ServeUrl + 'm=Announcement&a=getAnnouncement',
        method : 'post',
        cache : false,
        timeout : 30,
        dataType : 'json',
    }, function(ret, err) {
        if (ret) {
            // alert(JSON.stringify(ret));
            $('#noticeContent').html(ret.title);
        } else {
            alert(JSON.stringify(err));
        }
    });
}

//资讯列表加载
 function load(first, index2) {
     api.showProgress({
        style: 'default',
        animationType: 'fade',
        title: '拼命加载中...',
        text: '不如喝茶去...',
        modal: false
     });
     var index = $api.getStorage("daily_index");
     var username = $api.getStorage("username");
     var url = ServeUrl + "m=Daily&a=dailyList&pageIndex=" + index + "&pageCount=15&username="+username;
     ajaxRequest(url, 'get', '', function(data) {
         dealWithHtml(data, first, index);
     });

 }

 function ajaxRequest(url, method, datas, callBack) {
     api.ajax({
         url : url,
         method : method,
         cache : false,
         timeout : 30,
         dataType : 'json',
         data : {
             values : datas
         }
     }, function(ret, err) {
         if (ret.data) {
             callBack(ret, err);
         } else {
             //alert("没有更多的日报！");
             toast.success({
                 title:"已加载所有日报",
                 duration:2000
             });
             api.hideProgress();
         }
     });
 }

//字符串拼接
 function dealWithHtml(data, first, index) {
     //alert(JSON.stringify(data));
     var html = '';
     for (var i = 0; i < data.data.length; i++) {
          if(data.data[i].user_img){
             user_img = 'http://101.37.91.134/include/data/headImg/'+data.data[i].user_img;
          }else {
             user_img = '../image/demo1.png';
          }
          html += '<li class="aui-list-item">'
          html += '<div class="aui-media-list-item-inner">'
          html += '<div class="aui-list-item-inner">'
          html += '<div class="aui-info  aui-margin-b-5" style="padding:0">'
          html += '<div class="aui-info-item">'
          html += '<img src="'+user_img+'" style="width:1rem" class="aui-img-round" />'
          html += '<span class="aui-margin-l-5">'+data.data[i].createname+'</span>'
          html += '</div>'
          html += '<div class="aui-info-item"></div>'
          html += '</div>'
          html += '<div style="margin-left:20px;">'+data.data[i].daily_content+'</div>'
          html += '</div>'
          html += '</div>'
          html += '<div class="aui-info aui-font-size-12" style="padding-top:0; padding-bottom:10">'
          html += '<div class="aui-info-item">'+data.data[i].createtime+'</div>'
          html += '<div class="aui-info-item">'
          html += '评论<i class="aui-iconfont aui-icon-comment" onclick="funOpenWin( this,'+data.data[i].id+' );" data-name="comment-frm" style="color:#4c5276"></i>'
          html += '</div>'
          html += '</div>'
          html += '<section class="aui-content-padded" style="padding-bottom:5px;">'
          for (var j = 0; j < data.data[i].comment.length; j++) {
              html += '<p onclick="reply('+"'"+data.data[i].comment[j].parent.createname+"',"+data.data[i].comment[j].parent.id+','+data.data[i].id+')"><a>'+data.data[i].comment[j].parent.createname+'</a>:&nbsp;'+data.data[i].comment[j].parent.comment+'</p>'
              for (var k = 0; k < data.data[i].comment[j].child.length; k++) {
                  html += '<p onclick="reply('+"'"+data.data[i].comment[j].child[k].parent.createname+"',"+data.data[i].comment[j].parent.id+','+data.data[i].id+')"><a>'+data.data[i].comment[j].child[k].parent.createname+'</a>回复<a>'+data.data[i].comment[j].child[k].parent.receiveman+'</a>:&nbsp;'+data.data[i].comment[j].child[k].parent.comment+'</p>'
              }
          }
          html += '</section>'
          html += '</li>';
     }
    $("#daily-list").append(html);
    api.hideProgress();

 }

//弹出评论分享框架
function funOpenWin( tag,id ){
    var name = tag.getAttribute('data-name');
    $api.setStorage( 'backFrame' , name );  //设置需要关闭的frame
    api.openFrame({
        name: name,
        url : 'openWin.html',
        rect: {
            x: 0,
            y: 0,
            w: api.winWidth,
            h: api.winHeight
        },
        pageParam:{
            daily_id : id,
        }
    });
};

function reply(uname,id,daily_id){
    $api.setStorage( 'backFrame' , 'comment-reply-frm' );  //设置需要关闭的frame
    api.openFrame({
        name: 'comment-reply-frm',
        url : 'openWin.html',
        rect: {
            x: 0,
            y: 0,
            w: api.winWidth,
            h: api.winHeight
        },
        pageParam:{
           id : id,
           daily_id : daily_id,
           username : uname
        }
    });

}

function openWin(name) {
    api.openWin({
        name: name,
        url: name+'_win.html',
    });
}
</script>
</html>
